package com.baitiaojun.system.admin.service.auth.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baitiaojun.common.enums.impl.ServiceExpEnums;
import com.baitiaojun.common.enums.impl.UserStatusEnums;
import com.baitiaojun.common.exception.ServiceException;
import com.baitiaojun.common.utils.object.BeanUtils;
import com.baitiaojun.security.core.domain.LoginUser;
import com.baitiaojun.security.core.domain.Role;
import com.baitiaojun.security.core.domain.UserInfo;
import com.baitiaojun.security.core.service.PermissionService;
import com.baitiaojun.system.admin.mapper.auth.UserDetailsMapper;
import com.baitiaojun.system.admin.pojo.SysRoleDO;
import com.baitiaojun.system.admin.pojo.SysUserDO;
import com.baitiaojun.system.admin.service.auth.AuthUserDetailsService;
import com.baitiaojun.system.admin.service.permission.AuthPermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Set;

/**
 * 登陆用户信息和权限查询
 */
@Slf4j
@Service
public class AuthUserDetailsServiceImpl extends ServiceImpl<UserDetailsMapper, SysUserDO> implements AuthUserDetailsService {

    @Autowired
    private UserDetailsMapper userDetailsMapper;

    @Autowired
    private AuthPermissionService authPermissionService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //查询用户信息
        SysUserDO sysUserDO = userDetailsMapper.selectOne(username);
        ServiceExpEnums serviceExpEnums;
        if (sysUserDO == null) {
            log.warn("登录用户：{} 不存在.", username);
            serviceExpEnums = ServiceExpEnums.EXP_LOGIN_USER_NOT_EXISTS;
            throw new ServiceException(serviceExpEnums.getCode(), serviceExpEnums.getName() + "'" + username + "'");
        } else if (String.valueOf(UserStatusEnums.DELETED.getCode()).equals(sysUserDO.getDelFlag())) {
            log.warn("登录用户：{} 已被删除.", username);
            serviceExpEnums = ServiceExpEnums.EXP_LOGIN_USER_DELETED;
            throw new ServiceException(serviceExpEnums.getCode(), serviceExpEnums.getName() + "'" + username + "'");
        } else if (String.valueOf(UserStatusEnums.DISABLE.getCode()).equals(sysUserDO.getStatus())) {
            log.warn("登录用户：{} 已被停用.", username);
            serviceExpEnums = ServiceExpEnums.EXP_LOGIN_USER_DISABLE;
            throw new ServiceException(serviceExpEnums.getCode(), serviceExpEnums.getName() + "'" + username + "'");
        }
        UserInfo userInfo = BeanUtil.copyProperties(sysUserDO, UserInfo.class);

        String userId = String.valueOf(userInfo.getUserId());

        //查询用户权限
        Set<String> perms = authPermissionService.getPermsByUserId(userId, PermissionService.ADMIN.equals(sysUserDO.getUserName()));
        //查询用户角色
        Set<SysRoleDO> roles = authPermissionService.getRolesByUserId(userId);
        userInfo.setRoles(BeanUtils.toBeanList(roles, Role.class));
        return new LoginUser(userInfo, perms);
    }
}
